package com.xsq.page;

import android.app.Presentation;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.Display;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xsq.BaseApplication;
import com.xsq.R;
import com.xsq.data.CardInfo;

public class ShopperPage extends Presentation {

    private TextView timeDate;
    private TextView timeTime;
    private TextView textMoney;
    private TextView textUserName;
    private TextView textUserCardNo;
    private TextView textUserMoney;
    private TextView textUserCardStatus;

    private LinearLayout dialog;
    private TextView tvMessage;
    private static int MSG_DISMISS_DIALOG = 0;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(MSG_DISMISS_DIALOG == msg.what){
                if(null != dialog){
                    dialog.setVisibility(View.GONE);
                }
            }
        }
    };

    /*** 不会自动关闭的 ***/
    protected void showDialog(String loadingText) {
        if(dialog != null) {
            dialog.setVisibility(View.VISIBLE);
            if(tvMessage != null) {
                tvMessage.setText(loadingText);
            }
        }
    }

    /*** 会自动关闭的（延迟多长时间） ***/
    protected void showDialog(String loadingText, long delayCloseMillis) {
        if(dialog != null) {
            dialog.setVisibility(View.VISIBLE);
            if(tvMessage != null) {
                tvMessage.setText(loadingText);
            }
        }
        mHandler.sendEmptyMessageDelayed(MSG_DISMISS_DIALOG, delayCloseMillis);
    }

    public ShopperPage(Context outerContext, Display display) {
        super(outerContext,display);
        //TODOAuto-generated constructor stub
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main_shopper);
        initView();
        if(BaseApplication.getInstance().getPriceType() == BaseFragment.PriceType.Auto) {
            String autoPrice = BaseApplication.getInstance().getAutoPrice();
            if(!TextUtils.isEmpty(autoPrice)) {
                textMoney.setText(autoPrice);
            }
        }
        new ClockThread().start();
    }

    private void initView() {
        timeDate = findViewById(R.id.time_date);
        timeTime = findViewById(R.id.time_time);
        textMoney = findViewById(R.id.text_money);
        textUserName = findViewById(R.id.text_user_name);
        textUserCardNo = findViewById(R.id.text_user_card_no);
        textUserMoney = findViewById(R.id.text_user_money);
        textUserCardStatus = findViewById(R.id.text_user_card_status);
        dialog = findViewById(R.id.ll);
        tvMessage = findViewById(R.id.tv_message);
    }

    class ClockThread extends Thread {
        @Override
        public void run() {
            do {
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = 1;  //消息(一个整型值)
                    clockHandler.sendMessage(msg);// 每隔1秒发送一个msg给clockHandler
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (true);
        }
    }

    //【时钟的】 在主线程里面处理消息并更新UI界面
    private Handler clockHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    long sysTime = System.currentTimeMillis();//获取系统时间
                    CharSequence sysDateStr = DateFormat.format("yyyy年MM月dd日", sysTime);//时间显示格式
                    CharSequence sysTimeStr = DateFormat.format("HH:mm:ss", sysTime);//时间显示格式
                    timeDate.setText(sysDateStr); //更新时间
                    timeTime.setText(sysTimeStr); //更新时间
                    break;
                default:
                    break;

            }
        }
    };

    public void updateOrderMoney(String orderMoney) {
        if(BaseApplication.getInstance().getPriceType() != BaseFragment.PriceType.Auto) {
            textMoney.setText(orderMoney);
        }
    }

    public void updateUserInfo(CardInfo cardInfo) {
        if(cardInfo == null) {
            textUserName.setText("");
            textUserCardNo.setText("");
            textUserMoney.setText("");
            textUserCardStatus.setText("");
        }else {
            textUserName.setText(cardInfo.getUSERNAME());
            textUserCardNo.setText(cardInfo.getCARDNO());
            textUserMoney.setText(cardInfo.getACCOUNTBALANCE().toString());
            String cardStatus = "";
            switch (cardInfo.getCARDSTATUS()) {
                case -1:
                    cardStatus = "未发卡";
                    break;
                case 0:
                    cardStatus = "停用";
                    break;
                case 1:
                    cardStatus = "正常";
                    break;
                case 2:
                    cardStatus = "解挂";
                    break;
                case 3:
                    cardStatus = "挂失";
                    break;
                case 4:
                    cardStatus = "换卡";
                    break;
            }
            textUserCardStatus.setText(cardStatus);
        }
    }

    public void updateDialog(String message, long time) {
        if(time <= 0) {
            showDialog(message);
        }else {
            showDialog(message, time);
        }
    }
}
